<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>管理员登录</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
      background-color: #f5f5f5;
    }

    .login-container {
      background-color: white;
      padding: 30px;
      border-radius: 5px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      width: 350px;
    }

    h1 {
      text-align: center;
      margin-bottom: 20px;
    }

    .form-group {
      margin-bottom: 15px;
    }

    label {
      display: block;
      margin-bottom: 5px;
    }

    input {
      width: 100%;
      padding: 8px;
      border: 1px solid #ddd;
      border-radius: 4px;
      box-sizing: border-box;
    }

    button {
      width: 100%;
      padding: 10px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    button:hover {
      background-color: #45a049;
    }

    .message {
      margin-top: 15px;
      padding: 10px;
      border-radius: 4px;
      display: none;
    }

    .error {
      background-color: #ffebee;
      color: #c62828;
      display: block;
    }

    .success {
      background-color: #e8f5e9;
      color: #2e7d32;
      display: block;
    }
  </style>
</head>

<body>
  <div class="login-container">
    <h1>管理员登录</h1>
    <form id="loginForm">
      <div class="form-group">
        <label for="username">用户名</label>
        <input type="text" id="username" name="username">
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input type="password" id="password" name="password">
      </div>
      <button type="submit">登录</button>
    </form>
    <div id="message" class="message"></div>
  </div>

  <script>
    document.getElementById('loginForm').addEventListener('submit', async function (e) {
      e.preventDefault();

      const username = document.getElementById('username').value;
      const password = document.getElementById('password').value;
      const messageEl = document.getElementById('message');

      messageEl.textContent = '正在登录...';
      messageEl.className = 'message';
      messageEl.style.display = 'block';

      try {
        console.log('发送登录请求', { username, password });
        const response = await fetch('/api/admin/login', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({ username, password })
        });

        console.log('响应状态:', response.status);
        const data = await response.json();
        console.log('响应数据:', data);

        if (data.success) {
          messageEl.textContent = '登录成功，正在跳转...';
          messageEl.className = 'message success';
          localStorage.setItem('adminToken', data.token);
          setTimeout(() => {
            window.location.href = 'admin.html';
          }, 1000);
        } else {
          messageEl.textContent = data.message || '登录失败';
          messageEl.className = 'message error';
        }
      } catch (error) {
        console.error('登录错误:', error);
        messageEl.textContent = '网络错误，请稍后重试';
        messageEl.className = 'message error';
      }
    });
  </script>
</body>

</html>